function Swiper(el, options) {

    options = $.extend({
        navigation: {}
    }, options)

    this.$swiper = $(el)

    if (this.$swiper.length < 1) {
        return
    }

    this.$swiperWrapper = this.$swiper.find(options.wrapperClass || '.swiper-wrapper')
    this.$swiperItem = this.$swiperWrapper.find(options.slideClass || '.swiper-slide')
    this.$prevBtn = $(options.navigation.prevEl)
    this.$nextBtn = $(options.navigation.nextEl)
    this.$indexInput = $('.index-input')
    this.swiperCount = this.$swiperItem.length
    this.activeIndex = 0
    this.swiperHeight = this.$swiper.height()
    this.animating = false
    this.translate = -this.activeIndex * this.swiperHeight
    this.distanceThreshold = this.swiperHeight / 2 // 最大距离
    this.panDistanceInTransitionEnd = 0
    this.velocityThreshold = 0.3 // 翻页速度
    this.defaultSpeed = 0.3
    this.isInited = false // 判断是否进行过初始化, 为了避免 window 窗口的放大缩小 drag: false,

    this.bindEvents();

}

Swiper.prototype = {

    // 数据初始化，如果整个屏幕的大小发生了变化
    reload() {
        this.animating = false
        this.swiperHeight = this.$swiper.height()
        this.translate = -this.activeIndex * this.swiperHeight
        this.velocityThreshold = 0.3
        this.distanceThreshold = this.swiperHeight / 2
        this.panDistanceInTransitionEnd = 0
    },

    // 绑定事件
    bindEvents() {

        var _this = this;
        var y0 = 0; // 刚开始的 纵轴坐标
        var deltaY = 0; // 纵向移动的距离
        var time0 = 0; // 移动开始到结束用了多久
        var isMobile=Utils.isMobile()  // pc 还是 mobile

        this.$swiperWrapper.on('transitionend webkitTransitionEnd', this.transitionEndHandle.bind(this))
        
        /**
         * 移动开始
         * 需要记录时间、当前y坐标
         */
        this.$swiper.on(isMobile? 'touchstart' : 'mousedown', function (ev) {

            // console.log("start")

            // 开发中发现：transitionend 在某些情况下未被触发，导致 animating 一直为 true，从而拒绝用户响应
            // 因此，需要获取当前 translateY 的值，判断其是否处于 activeIndex 的初始位置，若是则代表过渡完毕
            var translate = Utils.getTranslate(_this.$swiperWrapper[0], 'y')
            
            console.log(translate,-_this.activeIndex * _this.swiperHeight)

            if (translate === -_this.activeIndex * _this.swiperHeight) {

                drag = _this.$swiper
                y0 = _this.getFirstFingerY(ev)
                time0 = new Date()
                _this.animating = false

            } else {
                drag = false
            }

        })

        // 移动结束
        this.$swiper.on(isMobile ? 'touchend' : 'mouseup', function (ev) {

            var interval = new Date() - time0
           drag = false

            _this.panEnd(deltaY, deltaY / interval)

        })

        // 移动
        this.$swiper.on(isMobile? 'touchmove' : 'mousemove', function (ev) {
            if (drag) {
                deltaY = _this.getFirstFingerY(ev) - y0
                _this.pan(deltaY)
            }
        })

        // 前一张
        this.$prevBtn.on('click', function () {
            _this.slidePrev()
        })

        // 后一张
        this.$nextBtn.on('click', function () {
            _this.slideNext()
        })

        this.$indexInput.on('input', function (ev) {

            var inputVal = $(ev.currentTarget).val()

            if (inputVal >= 0 && inputVal <= _this.swiperCount - 1) {
                _this.slideTo(inputVal)
            }
        })

        // 如果已经绑定过了，就不重复绑定了
        if (!this.isInited) {
            window.addEventListener("resize", function () {
                _this.reload()
                _this.slideTo()
                _this.removeEvents()
                _this.bindEvents()
            }, false);
        }

        this.isInited = true

    },

    // 移除事件，当应用平台类型变化，需要重新绑定事件，那么就需要先解除事件
    removeEvents() {
        this.$swiper.off('touchstart')
        this.$swiper.off('mousedown')
        this.$swiper.off('touchend')
        this.$swiper.off('mouseup')
        this.$swiper.off('touchmove')
        this.$swiper.off('mousemove')
        this.$swiper.off('transitionend webkitTransitionEnd')
        this.$prevBtn.off('click')
        this.$nextBtn.off('click')
        this.$indexInput.off('input')
    },

    // 获取正确的移动的纵轴距离，手机参数和 pc 纵向移动距离参数不一样
    getFirstFingerY(ev) {
        return Utils.isMobile() ? ev.touches[0].pageY : event.pageY
    },

    // 设置移动距离的 过滤函数
    pan(deltaY) {

        if (this.animating) {

            // 过渡未完成时，就产生拖拽的距离
            // 处理首屏频繁多次往下拖拽或最后一屏往上拖拽的问题
            this.panDistanceInTransitionEnd = ev.deltaY

            return
        }

        this.panSwiper(deltaY)

    },

    // 缓慢移动，跟着手势 纵向移动距离变化而变化
    panSwiper(deltaY) {
    	
//  	console.log(44444444444444)

        // 当前活跃块的位移 + 拖拽距离 - 过渡未完成时就产生拖拽的距离
        var curOffset = -this.activeIndex * this.swiperHeight
        var finalOffset = curOffset + deltaY - this.panDistanceInTransitionEnd

        this.$swiperWrapper.css({
            '-webkit-transform': 'translate3d(0, ' + finalOffset + 'px, 0)',
            'transform': 'translate3d(0, ' + finalOffset + 'px, 0)',
            '-webkit-transition': 'none',
            'transition': 'none'
        })

    },

    // 移动结束，开始判断滑动要怎么处理
    panEnd(deltaY, velocity) {

        if (this.animating) return

        this.panDistanceInTransitionEnd = 0
        var deltaY = deltaY
        var absDeltaY = Math.abs(deltaY)
        var absVelocity = Math.abs(velocity)
        var canSide=absDeltaY >= this.distanceThreshold || absVelocity >= this.velocityThreshold

        // 往上翻，就是往下滑
        if (deltaY > 0 && this.activeIndex > 0) {

            // 判断滑动距离和滑动速度是否满足指定条件
            if (canSide) {

                // 满足则 prev
                this.slidePrev()
                
            } else {

                // 不满足则回弹到原来位置
                this.slideTo(this.activeIndex)

            }

        }
        
        // 往下翻，就是往上滑
        else if (deltaY < 0 && this.activeIndex < this.swiperCount - 1) {

            if (canSide) {

                this.slideNext()
            } else {
                this.slideTo(this.activeIndex)
            }
        } 
        
        
        else {
            this.slideTo(this.activeIndex)
        }

    },

    // 动画完成，把标志位重置
    transitionEndHandle(ev) {
    	
    	console.log(ev.propertyName)

        if (['transform', 'webkitTransform'].indexOf(ev.propertyName) !== -1) {
            this.animating = false
        }

    },

    // 快速移动，自动翻页
    slideTo(index, speed) {

        var offset = -(index || this.activeIndex) * this.swiperHeight
        var speed = typeof speed === 'undefined' ? this.defaultSpeed : speed
        this.activeIndex = index || this.activeIndex

        if (speed > 0) {
            this.animating = true
        }

        this.translate = offset
        this.$swiperWrapper.css({
            '-webkit-transform': 'translate3d(0, ' + offset + 'px, 0)',
            'transform': 'translate3d(0, ' + offset + 'px, 0)',
            '-webkit-transition': 'transform ' + speed + 's',
            'transition': 'transform ' + speed + 's'
        })

    },

    // 上一张
    slidePrev(speed) {

        if (this.animating || this.activeIndex <= 0) return

        this.activeIndex--
        if (this.activeIndex < 0) {
            this.activeIndex = 0
        }
        this.slideTo(this.activeIndex, speed)
    },

    // 下一张
    slideNext(speed) {

        if (this.animating || this.activeIndex >= this.swiperCount - 1) return;

        this.activeIndex++;

        if (this.activeIndex >= this.swiperCount - 1) {
            this.activeIndex = this.swiperCount - 1
        }

        this.slideTo(this.activeIndex, speed)

    }

}


// module.exports = Swiper